﻿using System.Collections.Generic;
using System.Linq;
using Gumini.JulaDB.DataTypes;

namespace Gumini.JulaDB.Expressions.Functions
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    internal class SumFunction : AggregateFunction
    {
        public SumFunction()
            : base("sum") { }

        public override object Evaluate(List<TypedValue> values)
        {
            values = values.Where(v => !v.IsNull).ToList();
            if (values.Count == 0)
                return null;
            return values.Sum(v => (decimal)v.Value);
        }

        public override DataType GetReturnType()
        {
            return DataType.Number;
        }
    }
}
